package com.dream.technology.components.expresion.imp;

import com.dream.technology.components.context.FlowContext;
import com.dream.technology.components.expresion.ExpressionService;
import com.dream.technology.db.entity.RcOperationalSymbol;
import com.dream.technology.db.entity.vo.RiskItemVo;
//import com.dream.technology.zycashLog.Console;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 描述：数值比较
 *
 * @author chen_q_i@163.com
 * 2018/12/6 : 14:06.
 * @version : 1.0
 */
@Slf4j
@Service("riskNumberExpressionAnalysis")
public class RiskNumberExpressionAnalysis implements ExpressionService {

    @Autowired
    private Map<String, RcOperationalSymbol> symbolMap;

    /**
     * 解析每个元素 ， 生成不同的表达式并计算 返回boolean 值
     *
     * @param flowContext
     * @param item
     * @return
     */
    @Override
    public boolean expressionAnalysis(FlowContext flowContext, RiskItemVo item) {

        // 构建表达式
        String buildExpression = buildExpression(flowContext, item);
        Boolean aBoolean = analysisExpression(buildExpression);
        log.info("执行节点 ：{} 表达式 ：{} 结果 ：{}",item.getFieldName(),buildExpression, aBoolean);
        return aBoolean;
    }


    /**
     * 构建表达式
     *
     * @param flowContext
     * @param item
     * @return
     */
    String buildExpression(FlowContext flowContext, RiskItemVo item) {
        // 构建表达式
        String symbolCode = item.getOperationalSymbolCode();
        String fieldCode = item.getFieldCode();
        String fieldValue = item.getFieldValue();
        String o =String.valueOf( flowContext.get(fieldCode));
        //处理区间
        if (symbolCode.contains(",")) {
            String[] split = symbolCode.split(",");
            String[] values = fieldValue.split(",");
            // 符号替换
            RcOperationalSymbol rcOperationalSymbol = symbolMap.get(split[0]);
            String value = rcOperationalSymbol.getValue();
            if (split.length < 2) {
                return o + value + values[0];
            } else {
                RcOperationalSymbol symbolTow = symbolMap.get(split[1]);
                return o + value + values[0] + "&" + o + symbolTow.getValue() + values[1];
            }
        } else {
            //非区间处理
            // 符号替换
            RcOperationalSymbol rcOperationalSymbol = symbolMap.get(symbolCode);
            // 值替换
            return o + rcOperationalSymbol.getValue() + fieldValue;
        }


    }

}
